<?php
/**
 * Plugin Name: mlink-widget
 * Plugin URI: http://code.google.com/p/mlink-widget/
 * Description: A WordPress plugin for integrating a mLink.cz links
 * Author: syntaxsugar
 * Author URI: http://syntaxsugar.cz
 *
 */

add_action( 'widgets_init', 'mlink_load_widgets' );

function mlink_load_widgets() { register_widget( 'Mlink_Widget' ); }

class mlink_widget extends WP_Widget {

    var $BaseUrl = 'http://www.mlink.cz/';
    var $connectionTimeout  = 10;
    
	function mlink_widget() {
		$widget_ops = array( 'classname' => '', 'description' => __('Konektor do mLink.cz') );
       		
		$this->WP_Widget('widget', 'mLink.cz', $widget_ops);	
    	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['block'] = strip_tags( $new_instance['block'] );
		$instance['template'] = $new_instance['template'];

		return $instance;
	}
	
	function form( $instance ) {

		$defaults = array( 'title' => '', 'template' => '<div>%1$s</div>');

		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'block' ); ?>"><?php _e('Block:', 'mlink'); ?></label>
			<input id="<?php echo $this->get_field_id( 'block' ); ?>" name="<?php echo $this->get_field_name( 'block' ); ?>" value="<?php echo $instance['block']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'template' ); ?>"><?php _e('Template:', 'mlink'); ?></label>
			<input id="<?php echo $this->get_field_id( 'template' ); ?>" name="<?php echo $this->get_field_name( 'template' ); ?>" value="<?php echo htmlentities($instance['template']); ?>" style="width:100%;" />
		</p>

	<?php
	}
	
	function widget( $args, $instance ) {
		if ( isset($instance['error']) && $instance['error'] ) {
			return;
		}

		extract($args, EXTR_SKIP);

		$block = $instance['block'];
		if ( empty($block) ) {
			return;
		}

        $url = $this->BaseUrl . "showad.php?Url=" . urlencode(((isset($_SERVER['HTTPS'])&&($_SERVER['HTTPS']=='on'))?'https://':'http://'));
        $url .= urlencode($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); 
        $url .= "&block=" .urlencode($block);
        
        $data = $this->fetchFile($url);
                
		$title = $instance['title'];
		$title = apply_filters('widget_title', $title );

		echo $before_widget;

		if ($title) {
            echo $before_title . $title . $after_title;
        }
		
		printf((!empty($instance['template']) ? $instance['template'] : '<div>%1$s</div>'), $data);
	
		echo $after_widget;

		unset($data);
	}
	
    function fetchFile($url)
    {
        $results='';
        if (function_exists('curl_init')) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_ENCODING, 1);
            $results = curl_exec($ch);
            curl_close($ch);
        } else if (function_exists('file_get_contents')) {
            $results =  file_get_contents($url);
        } else {
            $url = parse_url($url);
            if ($handle = @fsockopen ($url["host"], 80)) {
                if (function_exists("socket_set_timeout")) {
                    socket_set_timeout($handle, $this->connectionTimeout, 0);
                } else if (function_exists("stream_set_timeout")) {
                    stream_set_timeout($handle, $this->connectionTimeout, 0);
                }
        
                fwrite($handle, "GET $url[path]?$url[query] HTTP/1.0\r\nHost: $url[host]\r\nConnection: Close\r\n\r\n");
                while (!feof($handle)) {
                    $results .= @fread($handle, 40960);
                }
                fclose($handle);
            }
        }
        return $results;
    }
}

?>
